package gingen

import (
	"fginTpl/gin_gen/config"
	genhandle "fginTpl/gin_gen/gen_handle"
	"fginTpl/gin_gen/globals"
	"fginTpl/utils"
	"fmt"
	"path/filepath"
)

func GenGin(projectName, path string) {
	config.ReadConfig(projectName, path)
	absPath := utils.GetAbsPath(config.ConfigData.Path)
	baseDir := filepath.Join(absPath, "./"+config.ConfigData.ProjectName)
	globals.BASEDIR = baseDir
	// 检查如果已存在，则不进行覆盖
	if utils.FileOrDirExists(baseDir) {
		fmt.Println("gin模版已存在, 路径为：", baseDir)
		return
	}
	// 全部目录路径
	dirs := []string{
		baseDir,
		filepath.Join(baseDir, "./config"),
		filepath.Join(baseDir, "./controllers"),
		filepath.Join(baseDir, "./init_plugs"),
		filepath.Join(baseDir, "./middles"),
		filepath.Join(baseDir, "./routers"),
		filepath.Join(baseDir, "./services"),
		filepath.Join(baseDir, "./utils"),
		filepath.Join(baseDir, "./view_models"),
		filepath.Join(baseDir, "./dao"),
		filepath.Join(baseDir, "./logs"),
	}
	// 不存在则新增目录
	for _, dirv := range dirs {
		if err := utils.MakeDirAll(dirv); err != nil {
			fmt.Printf("创建目录失败: %s, 错误: %s\n", dirv, err.Error())
			continue
		}
	}
	// 实现代码生成
	genhandle.GenAll()
	fmt.Println("gin模版创建完成, 路径为：", baseDir)
}
